package com.client;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

/**
 * Created by YQR on 2017/11/27.
 */

public class HttpClient {
	private static Log log=LogFactory.getLog(HttpClient.class);
//	private static String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36";

	
	/**
	 * doget
	 * @param 
	 * 
	 */
	public static String  doget(String u,String p) throws URISyntaxException, ClientProtocolException, IOException{
		//创建客户端对象
				CloseableHttpClient client = HttpClients.createDefault();
//				使用 URIBuilder类构造get类型的url
				    URI uri;				
					uri = new URIBuilder()
							.setScheme("http")
							.setHost("192.168.1.67")
							.setPort(8080)
							.setPath("/wms/home")
							.setParameter("user", String.valueOf(u))
							.setParameter("pow", String.valueOf(p))
							.build();				
				//新建get类型的对象
				HttpGet get =new HttpGet(uri);
				//新建响应
				CloseableHttpResponse response =client.execute(get);
				String result="";
				//输出结果
				if(response.getStatusLine().getStatusCode()==200){
					result=EntityUtils.toString(response.getEntity());
			   }
				return result;
		}
	
	/**
	 * dopost
	 * 
	 */
	public  static String dopost() throws ClientProtocolException, IOException{
		//创建客户端对象
		CloseableHttpClient client = HttpClients.createDefault();
		//新建httppost对象	
		HttpPost post =new HttpPost("http://192.168.1.67:8080/wms/home");
		
		//使用NameValuePair对参数进行打包
		List<NameValuePair> list =new ArrayList<NameValuePair>();
		list.add(new BasicNameValuePair("u", "admin"));
		list.add(new BasicNameValuePair("pow","admin"));
		
		//对打包后的参数重现编码
		UrlEncodedFormEntity entity =new UrlEncodedFormEntity(list,Consts.UTF_8);		
		//将编码后的参数放到请求实体中
		post.setEntity(entity);
		
		//响应
		CloseableHttpResponse response= client.execute(post);
		
		String result="";
		//输出结果
		if(response.getStatusLine().getStatusCode()==200){
			result=EntityUtils.toString(response.getEntity());
		}
		
		return result;
		
	}
	
	/**
	 * dopostJSON
	 * @param   url
	 * @param  bodyParams
	 */
public static JSONObject doPostJson(String url,String bodyParams){
	    CloseableHttpClient HttpClient=null;
	    HttpPost post=null;
	    StringEntity se=null;
	    HttpResponse res=null;
	    JSONObject response = null;
	    HttpEntity entity =null;
	    
		//创建客户端
	    HttpClient = HttpClients.createDefault();
		//请求行
	    post=new HttpPost(url);
	    
		//输出：requst StatusLine
	    log.info("-----------------------------------请求行-----------------------");
	    log.info("请求行:"+post);
		
		try {
			//请求实体
			se = new StringEntity(bodyParams);
			se.setContentEncoding("UTF-8");
			se.setContentType("application/json");//发送json数据需要设置contentType			
		    post.setEntity(se);
	
		  //输出：requst Entity
		    log.info("-----------------------------------请求实体-----------------------");
		    log.info("请求实体:"+bodyParams);
		    
		    //响应行
		    res = HttpClient.execute(post);	  
		    //输出：response StatusLine
		    log.info("-----------------------------------响应行-----------------------");
		    log.info("响应行:"+res);
		    
				if(res.getStatusLine().getStatusCode()==200){			
			       entity = res.getEntity();			       			        
			        if(entity != null){			        
				        //响应
				        String result = EntityUtils.toString(entity,"UTF-8");//.toString(entity);
				        response = JSONObject.parseObject(result);//.parseObject(result);
				        //输出： response
				        log.info("-----------------------------------响应-----------------------");
				        log.info("响应:"+response.toString());	
				        
				        log.info("++++++++++++++++++++++++++++++++++++   end     ++++++++++++++++++++++++++++++++++++");
				        return response;	
			        }
			        
			     }else{
			    	 JSONObject reqFail = new JSONObject();	
			    	 reqFail.put("code", res.getStatusLine().getStatusCode());
			    	 reqFail.put("failbody", res.getStatusLine());			    	
			    	 return reqFail;
			     }
				
		    // 关闭流并释放资源
			HttpClient.close();	
			
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				log.error("HTTP post Error:{}",e);
			}		    	
		 return null;
		
	}
	
}
